package com.bluemsun.naturallanguageprocessing.service.impl;

import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.dao.FeedbackMapper;
import com.bluemsun.naturallanguageprocessing.entity.Feedback;
import com.bluemsun.naturallanguageprocessing.exception.feedback.AddFeedbackException;
import com.bluemsun.naturallanguageprocessing.exception.feedback.DeleteFeedbackException;
import com.bluemsun.naturallanguageprocessing.exception.feedback.GetFeedbackException;
import com.bluemsun.naturallanguageprocessing.service.IFeedbackService;
import com.bluemsun.naturallanguageprocessing.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;


@Service
public class FeedbackServiceImpl implements IFeedbackService {

    private static final Logger logger = LoggerFactory.getLogger(FeedbackServiceImpl.class);

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    @Transactional
    public ServerResponse userAddFeedBack(Feedback feedback) {
        int i = -1;
        try{
            i = feedbackMapper.addFeedback(feedback);
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            throw new AddFeedbackException(e.getMessage());
        }
        if (i == 1)
            return ServerResponse.createBySuccessMessage("意见反馈成功，感谢您的反馈");
        else
            throw new AddFeedbackException("Mybatis insert语句返回值异常，返回值为："+i);
    }

    @Override
    @Cacheable(cacheNames = {"FeedbackList"},key = "#userId")
    public PageInfo<Feedback> getFeedbackList(int userId,int pageNum,int pageSize) {
        List<Feedback> feedbackList = null;
        PageInfo<Feedback> of = null;
        try {
            PageHelper.startPage(pageNum,pageSize);
            feedbackList = feedbackMapper.getFeedback(userId);
            of = PageInfo.of(feedbackList);
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            throw new GetFeedbackException(e.getMessage());
        }
        return of;
    }

    @Override
    @Transactional
    public ServerResponse administratorDeleteFeedbackSet(List<Integer> ids) {
        int i = -1;
        try {
            i = feedbackMapper.deleteFeedback(ids);
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            throw new DeleteFeedbackException(e.getMessage());
        }
        if (i == 0){
            throw new DeleteFeedbackException("删除的反馈数据数量为："+i);
        }
        return ServerResponse.createBySuccess("删除反馈数据成功，共删除了"+i+"条数据");
    }

}
